﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.Models;
namespace RMS.Controllers
{
    public class CompanyController : Controller
    {
        RMSDataClassesDataContext db = new RMSDataClassesDataContext();

        public ActionResult OccurenceChart(int year,int companyId)
        {
            ViewData["year"] = year;
            ViewData["company"] = db.Companies.FirstOrDefault(x => x.Id == companyId);
            return View(db.ProblemGroups);
        }
        public ActionResult CostChart(int year,int companyId)
        {
            ViewData["year"] = year;
            ViewData["company"] = db.Companies.FirstOrDefault(x => x.Id == companyId);
            return View(db.ProblemGroups);
        }
        public ActionResult Overview(int id)
        {
            return View(db.Companies.FirstOrDefault(x=>x.Id == id));
        }
        public ActionResult EditCompanyLimit(int limitId, decimal limit)
        {
            
            CompanyProblemLimit lim = db.CompanyProblemLimits.FirstOrDefault(x=>x.Id == limitId);
            try
            {
                lim.Limit = limit;
                db.SubmitChanges();
            }
            catch { }
            return RedirectToAction("Details", new { id = lim.CompanyId });
        }
        public ActionResult Index()
        {
            return View(db.Companies);
        }
        public ActionResult Details(int id)
        {
            User user = db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name);
            if (user != null)
                ViewData["user"] = user;
            return View(db.Companies.FirstOrDefault(x => x.Id == id));
        }
        public ActionResult Create()
        {
            return View();
        } 
        [HttpPost]
        public ActionResult Create(Company newCompany)
        {
            try
            {
                db.Companies.InsertOnSubmit(newCompany);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Edit(int id)
        {
            return View(db.Companies.FirstOrDefault(x=>x.Id==id));
        }
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Company company = db.Companies.FirstOrDefault(x => x.Id == id);
            try
            {
                UpdateModel(company);
                db.SubmitChanges();
                return RedirectToAction("Details", new { id = id });
            }
            catch
            {
                return View(company);
            }
        }

        //
        // GET: /Company/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Company/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
